package com.netease.nie.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.netease.nie.base.exceptions.BusinessException;
import com.netease.nie.base.utils.CopyUtils;
import com.netease.nie.base.utils.DateUtils;
import com.netease.nie.constants.BusinessConstants;
import com.netease.nie.dto.ExchangeGoodsInfoListDto;
import com.netease.nie.dto.OrdersTraceDto;
import com.netease.nie.dto.RobGoodsInfoDto;
import com.netease.nie.dto.RobGoodsInfoExtendDto;
import com.netease.nie.dto.RobGoodsInfoPeriodDto;
import com.netease.nie.dto.RobGoodsPeriodPersonTimeDto;
import com.netease.nie.dto.RobGoodsPeriodUserPersonTimeDto;
import com.netease.nie.dto.TopicGoodsDto;
import com.netease.nie.model.ExchangeGoodsInfo;
import com.netease.nie.model.OrdersTrace;
import com.netease.nie.model.RobGoodsInfo;
import com.netease.nie.model.RobGoodsInfoExtend;
import com.netease.nie.model.RobGoodsInfoPeriod;
import com.netease.nie.model.RobGoodsPeriodPersonTime;
import com.netease.nie.model.Topic;
import com.netease.nie.model.TopicDto;
import com.netease.nie.model.TopicGoods;

/**
 * 
 * @author wwbn3695
 * @createDate June 24th,2016
 *
 */
public abstract class CommonService {
	
	protected int computeRemainDays(ExchangeGoodsInfo exchangeGoodsInfo, long currTimeMills) {
		if (exchangeGoodsInfo == null)
			throw new BusinessException(BusinessConstants.GOODS_NOT_EXISTS.getCode(), "商品不存在");
		Long putOffTime = exchangeGoodsInfo.getPutOffTime();
		int remainDays = 0;
		if (putOffTime != null) {
			remainDays = DateUtils.daysBetween(currTimeMills, exchangeGoodsInfo.getPutOffTime());
		}
		return remainDays;
	}
	
	protected List<ExchangeGoodsInfoListDto> generateGoodsInfoDtoList(List<ExchangeGoodsInfo> exchangeGoodsInfoList, long currTimeMills) {
		List<ExchangeGoodsInfoListDto> exchangeGoodsInfoListDtoList = new ArrayList<>();
		ExchangeGoodsInfo exchangeGoodsInfo = null;
		ExchangeGoodsInfoListDto exchangeGoodsInfoListDto = null;
		int size = exchangeGoodsInfoList.size();
		for (int i = 0; i < size; i++) {
			exchangeGoodsInfo = exchangeGoodsInfoList.get(i);
			exchangeGoodsInfoListDto = new ExchangeGoodsInfoListDto();
			CopyUtils.copy(exchangeGoodsInfoListDto, exchangeGoodsInfo);
			exchangeGoodsInfoListDto.setRemainDays(computeRemainDays(exchangeGoodsInfo, currTimeMills));
			exchangeGoodsInfoListDtoList.add(exchangeGoodsInfoListDto);
		}
		return exchangeGoodsInfoListDtoList;
	}
	
	protected List<RobGoodsInfoPeriodDto> generateRobGoodsInfoPeriodDtoList(List<RobGoodsInfoPeriod> robGoodsInfoPeriodList) {
		RobGoodsInfoPeriod robGoodsInfoPeriod = null;
		RobGoodsInfoPeriodDto robGoodsInfoPeriodDto = null;
		List<RobGoodsInfoPeriodDto> robGoodsInfoPeriodDtoList = new ArrayList<RobGoodsInfoPeriodDto>();
		if (robGoodsInfoPeriodList != null && !robGoodsInfoPeriodList.isEmpty()) {
			int robGoodsInfoPeriodListSize = robGoodsInfoPeriodList.size();
			for (int i = 0; i < robGoodsInfoPeriodListSize; i++) {
				robGoodsInfoPeriod = robGoodsInfoPeriodList.get(i);
				robGoodsInfoPeriodDto = new RobGoodsInfoPeriodDto();
				CopyUtils.copy(robGoodsInfoPeriodDto, robGoodsInfoPeriod);
				robGoodsInfoPeriodDtoList.add(robGoodsInfoPeriodDto);
			}
		}
		return robGoodsInfoPeriodDtoList;
	}
	
	protected List<RobGoodsInfoDto> generateRobGoodsInfoDtoList(List<RobGoodsInfo> robGoodsInfoList) {
		if (robGoodsInfoList != null && !robGoodsInfoList.isEmpty()) {
			List<RobGoodsInfoDto> robGoodsInfoDtoList = new ArrayList<RobGoodsInfoDto>();
			RobGoodsInfo robGoodsInfo = null;
			RobGoodsInfoDto robGoodsInfoDto = null;
			int robGoodsInfoListSize = robGoodsInfoList.size();
			for (int i = 0; i < robGoodsInfoListSize; i++) {
				robGoodsInfo = robGoodsInfoList.get(i);
				robGoodsInfoDto = new RobGoodsInfoDto();
				CopyUtils.copy(robGoodsInfoDto, robGoodsInfo);
				robGoodsInfoDto.setPrice(robGoodsInfo.getEachPrice());
				robGoodsInfoDtoList.add(robGoodsInfoDto);
			}
			return robGoodsInfoDtoList;
		}
		return Collections.emptyList();
	}
	
	protected Map<Integer, RobGoodsInfoDto> generateRobGoodsInfoDtoMap(List<RobGoodsInfo> robGoodsInfoList) {
		if (robGoodsInfoList != null && !robGoodsInfoList.isEmpty()) {
			Map<Integer, RobGoodsInfoDto> robGoodsInfoDtoMap = new HashMap<Integer, RobGoodsInfoDto>();
			RobGoodsInfo robGoodsInfo = null;
			RobGoodsInfoDto robGoodsInfoDto = null;
			int robGoodsInfoListSize = robGoodsInfoList.size();
			for (int i = 0; i < robGoodsInfoListSize; i++) {
				robGoodsInfo = robGoodsInfoList.get(i);
				robGoodsInfoDto = new RobGoodsInfoDto();
				CopyUtils.copy(robGoodsInfoDto, robGoodsInfo);
				robGoodsInfoDtoMap.put(robGoodsInfoDto.getId(), robGoodsInfoDto);
			}
			return robGoodsInfoDtoMap;
		}
		return Collections.emptyMap();
	}
	
	protected RobGoodsInfoDto generateRobGoodsInfoDto(RobGoodsInfo robGoodsInfo) {
		if (robGoodsInfo != null) {
			RobGoodsInfoDto robGoodsInfoDto = new RobGoodsInfoDto();
			CopyUtils.copy(robGoodsInfoDto, robGoodsInfo);
			return robGoodsInfoDto;
		}
		return null;
	}
	
	protected RobGoodsInfoExtendDto generateRobGoodsInfoExtendDto(RobGoodsInfoExtend robGoodsInfoExtend) {
		if (robGoodsInfoExtend != null) {
			RobGoodsInfoExtendDto robGoodsInfoExtendDto = new RobGoodsInfoExtendDto();
			CopyUtils.copy(robGoodsInfoExtendDto, robGoodsInfoExtend);
			return robGoodsInfoExtendDto;
		}
		return null;
	}
	
	protected List<RobGoodsPeriodUserPersonTimeDto> generateRobGoodsPeriodUserPersonTimeDtoList(List<RobGoodsPeriodPersonTime> robGoodsPeriodPersonTimeList) {
		if (robGoodsPeriodPersonTimeList != null && !robGoodsPeriodPersonTimeList.isEmpty()) {
			RobGoodsPeriodPersonTime robGoodsPeriodPersonTime = null;
			RobGoodsPeriodUserPersonTimeDto robGoodsPeriodUserPersonTimeDto = null;
			List<RobGoodsPeriodUserPersonTimeDto> robGoodsPeriodUserPersonTimeDtoList = new ArrayList<RobGoodsPeriodUserPersonTimeDto>();
			int robGoodsPeriodPersonTimeListSize = robGoodsPeriodPersonTimeList.size();
			for (int i = 0; i < robGoodsPeriodPersonTimeListSize; i++) {
				robGoodsPeriodPersonTime = robGoodsPeriodPersonTimeList.get(i);
				robGoodsPeriodUserPersonTimeDto = new RobGoodsPeriodUserPersonTimeDto();
				CopyUtils.copy(robGoodsPeriodUserPersonTimeDto, robGoodsPeriodPersonTime);
				robGoodsPeriodUserPersonTimeDtoList.add(robGoodsPeriodUserPersonTimeDto);
			}
			return robGoodsPeriodUserPersonTimeDtoList;
		}
		return Collections.emptyList();
	}
	
	protected List<RobGoodsPeriodPersonTimeDto> generateRobGoodsPeriodPersonTimeDtoList(List<RobGoodsPeriodPersonTime> robGoodsPeriodPersonTimeList) {
		if (robGoodsPeriodPersonTimeList != null && !robGoodsPeriodPersonTimeList.isEmpty()) {
			RobGoodsPeriodPersonTime robGoodsPeriodPersonTime = null;
			RobGoodsPeriodPersonTimeDto robGoodsPeriodPersonTimeDto = null;
			List<RobGoodsPeriodPersonTimeDto> robGoodsPeriodPersonTimeDtoList = new ArrayList<RobGoodsPeriodPersonTimeDto>();
			int robGoodsPeriodPersonTimeListSize = robGoodsPeriodPersonTimeList.size();
			for (int i = 0; i < robGoodsPeriodPersonTimeListSize; i++) {
				robGoodsPeriodPersonTime = robGoodsPeriodPersonTimeList.get(i);
				robGoodsPeriodPersonTimeDto = new RobGoodsPeriodPersonTimeDto();
				CopyUtils.copy(robGoodsPeriodPersonTimeDto, robGoodsPeriodPersonTime);
				robGoodsPeriodPersonTimeDtoList.add(robGoodsPeriodPersonTimeDto);
			}
			return robGoodsPeriodPersonTimeDtoList;
		}
		return Collections.emptyList();
	}
	
	protected List<OrdersTraceDto> generateOrdersTraceDtos(List<OrdersTrace> ordersTraceList) {
		if (ordersTraceList != null && !ordersTraceList.isEmpty()) {
			List<OrdersTraceDto> ordersTraceDtoList = new ArrayList<>();
			OrdersTrace ordersTrace = null;
			OrdersTraceDto ordersTraceDto = null;
			int ordersTraceListSize = ordersTraceList.size();
			for (int i = 0; i < ordersTraceListSize; i++) {
				ordersTrace = ordersTraceList.get(i);
				ordersTraceDto = new OrdersTraceDto();
				CopyUtils.copy(ordersTraceDto, ordersTrace);
				ordersTraceDtoList.add(ordersTraceDto);
			}
			return ordersTraceDtoList;
		}
		return Collections.emptyList();
	}
	
	protected List<TopicDto> generateTopicDtos(List<Topic> topicList) {
		if (topicList != null && !topicList.isEmpty()) {
			List<TopicDto> topicDtoList = new ArrayList<>();
			Topic topic = null;
			TopicDto topicDto = null;
			int topicListSize = topicList.size();
			for (int i = 0; i < topicListSize; i++) {
				topic = topicList.get(i);
				topicDto = new TopicDto();
				CopyUtils.copy(topicDto, topic);
				topicDtoList.add(topicDto);
			}
			return topicDtoList;
		}
		return Collections.emptyList();
	}
	
	protected List<TopicGoodsDto> generateTopicGoodsDtos(List<TopicGoods> topicGoodsList) {
		if (topicGoodsList != null && !topicGoodsList.isEmpty()) {
			List<TopicGoodsDto> topicGoodsDtoList = new ArrayList<>();
			TopicGoods topicGoods = null;
			TopicGoodsDto topicGoodsDto = null;
			int topicGoodsListSize = topicGoodsList.size();
			for (int i = 0; i < topicGoodsListSize; i++) {
				topicGoods = topicGoodsList.get(i);
				topicGoodsDto = new TopicGoodsDto();
				CopyUtils.copy(topicGoodsDto, topicGoods);
				topicGoodsDtoList.add(topicGoodsDto);
			}
			return topicGoodsDtoList;
		}
		return Collections.emptyList();
	}
}
